#!/usr/local/bin/perl
#
# This takes in a Scottie formated TeX file and creates the 
# framework for a Beamer presentation.

$source_file = 'open_ended_wg_solution_article_bois_critique.tex';     # Name the file
@line_pieces = split(/\.tex/, $source_file);
$output_file = $line_pieces[0] . '_presentation.tex';
print("$output_file \n");

open(source, $source_file);       # Open the file
open(output, ">$output_file");

####################################
# Print generic Beamer header
####################################
# (bug where '% B' translates to '0' - wtf?)
open(beamer_header, 'generic_beamer_header.tex');       # Open the file
$line = <beamer_header>;
while($line ne ""){
	printf output $line;
    $line = <beamer_header>;
}
printf output "\n";

####################################
# Scan file until title is found
####################################
$line = <source>;
$title = "";
while($line ne "" && $title eq ""){
	$old_line = $line;
    @line_pieces = split(/\\title/, $line);
	$title = $line_pieces[1];
	$line = <source>; 
}

if($title eq ""){
	print("No title found!\n");
}
else{
	printf output $old_line;
	if($old_line =~ /\[/){
		if($old_line =~ /\]/){
			if($old_line =~ /{/){
				if($old_line =~ /}/){
				
				}
				else{
					if($line =~ /}/){
					printf output $line;
					$line = <source>;}
				}
			}
			else{
				if($line =~ /{/){
					printf output $line;
					if($line =~ /}/){ 
					}
					else{
						$line = <source>;
						if($line =~ /}/){ 
						printf output $line;}
					}
				}
			}
		}
		else{
			if($line =~ /\]/){
				printf output $line;
				if($line =~ /{/){
					if(line =~ /}/){
					}		
					else{
						$line = <source>;
						if($line =~ /}/){
						printf output $line;}
					}	
				}
				else{
					$line = <source>;
					if($line =~ /{/){ 
						printf output $line;
						if($line =~ /}/){ 
						}
						else{
							$line = <source>;
							if($line =~ /}/){ 
							printf output $line;}
						}
					}
				}
			}
		}
	}
	elsif($old_line =~ /{/){
		if($old_line =~ /}/){
		
		}
		else{
			if($line =~ /}/){
			printf output $line;
			$line = <source>;}
		}
	}
}
printf output "\n";

#  Generic midsection for beginning document
printf output "\n"; printf output "\n"; 
printf output '\AtBeginSection[] {'; printf output "\n";
printf output '  \begin{frame}<beamer>'; printf output "\n";
printf output '    \frametitle{Outline}'; printf output "\n";
printf output '    \tableofcontents[currentsection]'; printf output "\n";
printf output '  \end{frame}'; printf output "\n";
printf output '}'; printf output "\n";

#printf output '\begin{document}'; printf output "\n";
printf output '\definecolor{eqcolor}{rgb}{0.0,0.1,0.5}'; printf output "\n";
printf output '\definecolor{darkblue}{rgb}{0.0,0.2,0.5}'; printf output "\n";
printf output '\definecolor{yellow}{rgb}{0.9,0.6,0}'; printf output "\n";
printf output '\definecolor{darkred}{rgb}{1,0,0.2}'; printf output "\n";
printf output '\definecolor{purple}{rgb}{0.5,0,1}'; printf output "\n";
printf output '%\pagecolor{darkblue} \color{lyellow}'; printf output "\n";

printf output '\begin{frame}'; printf output "\n";
printf output '  \titlepage'; printf output "\n";
printf output '  \insertdate	'; printf output "\n";
printf output '\end{frame}'; printf output "\n";
printf output "\n";

$line = <source>;
while($line ne ""){
	$old_line = $line;
    @line_pieces = split(/\\section/, $line);
	if($line_pieces[1] ne ""){
		printf output $line;
		printf output "\\begin\{frame\}\n";
		
		@section_line = split(/\}/, $line_pieces[1]);
		$frame_title = '\frametitle' . $line_pieces[1]. "\n";
		printf output $frame_title;
		printf output "\\end\{frame\}\n\n";
	}
	$line = <source>; 
}

printf output "\\end\{document\}\n";

print("File printed to $output_file\n");
close(source);            # Close the file
close(output);          # Close the file
